package com.example.kotlin.activity

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.example.kotlin.R
import kotlinx.android.synthetic.main.activity_third.sendMyBoradcast

class ThirdActivity : BaseActivity(), View.OnClickListener {

    lateinit var timeChangeReceiver: TimeChangeReceiver

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_third)

        sendMyBoradcast.setOnClickListener(this)

        val intentFiler=IntentFilter()
        intentFiler.addAction("android.intent.action.TIME_TICK")
        timeChangeReceiver=TimeChangeReceiver()
        registerReceiver(timeChangeReceiver,intentFiler)
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(timeChangeReceiver)
    }

    inner class TimeChangeReceiver:BroadcastReceiver(){
        override fun onReceive(context: Context?, intent: Intent?) {
           Toast.makeText(context,"时间更改了",Toast.LENGTH_SHORT).show()
        }
    }

    override fun onClick(view: View?) {
        when(view){
            sendMyBoradcast->{
                val intent=Intent("com.example.kotlin.My_BROADCAST")
                intent.setPackage(packageName)
                sendOrderedBroadcast(intent,null)
            }
        }
    }
}